package com.zlm.udp;

import java.net.*;

/**
 * @Author: 郑
 *
 */
public class UdpClientDemo1 {

    public static void main(String[] args) throws Exception {
        //建立一个socket
        DatagramSocket datagramSocket = new DatagramSocket(9091);
        //建立一个数据包
        //地址
        InetAddress localhost = InetAddress.getByName("localhost");
        String msg = "你好啊";
        DatagramPacket data = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, 9090);
        //发送包
        datagramSocket.send(data);
        //创建接受数据的包
        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
        datagramSocket.receive(datagramPacket);

        //输出接受到的数据
        System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
        //关闭
        datagramSocket.close();
    }
}
